package com.dingqb.util;

import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.Reader;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

public abstract class SystemUtils {

	@SuppressWarnings("unused")
	private static final String TAG = SystemUtils.class.getSimpleName();

	/**
	 * 获取版本 versionCode 102
	 * 
	 * @param context
	 * @return
	 */
	public static String getApkVersion(Context context) {
		PackageManager manager = context.getPackageManager();
		try {
			PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
			return String.valueOf(info.versionCode);
		} catch (NameNotFoundException e) {
		}
		return null;
	}

	/**
	 * 获取版本 versionName 1.0.2
	 * 
	 * @param context
	 * @return
	 */
	public static String getApkVersionName(Context context) {
		PackageManager manager = context.getPackageManager();
		try {
			PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
			return String.valueOf(info.versionName);
		} catch (NameNotFoundException e) {
		}
		return null;
	}

	/**获取Mac地址
	 * 
	 * @param context
	 * @return
	 */
	public static String getMacAdd(Context context) {
		String macAddress = null;
		if (Integer.valueOf(android.os.Build.VERSION.SDK) >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
			macAddress = getMac(context);
		} else {
			macAddress = getMacAddress(context);
		}
		return macAddress;

	}

	/**
	 * 6.0系统使用
	 * 
	 * @param context
	 * @return
	 */
	public static String getMac(Context context) {
		String str = "";
		String macSerial = "";
		int isWifiAvailable = 1;
		WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		if (!wifiManager.isWifiEnabled()) {
			// 必须先打开，才能获取到MAC地址
			isWifiAvailable = 0;
			wifiManager.setWifiEnabled(true);
		}
		try {
			Process pp = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address ");
			InputStreamReader ir = new InputStreamReader(pp.getInputStream());
			LineNumberReader input = new LineNumberReader(ir);

			for (; null != str;) {
				str = input.readLine();
				if (str != null) {
					macSerial = str.trim();// 去空格
					// wifi在打开状态且原来wifi不可用
					if (wifiManager.isWifiEnabled() && isWifiAvailable == 0) {
						wifiManager.setWifiEnabled(false);
					}
					break;
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		if (macSerial == null || "".equals(macSerial)) {
			try {
				String add = loadFileAsString("/sys/class/net/eth0/address").toUpperCase().substring(0, 17);
				if (wifiManager.isWifiEnabled() && isWifiAvailable == 0) {
					wifiManager.setWifiEnabled(false);
				}
				return add;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return macSerial;
	}

	/**
	 * 通过wifi获取Android系统MAC地址(需要开机以来至少打开一次wifi)本方法在android6.0不管用
	 * 
	 * @param context
	 * @return
	 */
	public static String getMacAddress(Context context) {
		String macAddress = null;
		WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		WifiInfo info = (null == wifiManager ? null : wifiManager.getConnectionInfo());
		if (!wifiManager.isWifiEnabled()) {
			// 必须先打开，才能获取到MAC地址
			wifiManager.setWifiEnabled(true);
			wifiManager.setWifiEnabled(false);
		}
		if (null != info) {
			macAddress = info.getMacAddress();
		}
		return macAddress;
	}

	private static String loadFileAsString(String fileName) throws Exception {
		FileReader reader = new FileReader(fileName);
		String text = loadReaderAsString(reader);
		reader.close();
		return text;
	}

	private static String loadReaderAsString(Reader reader) throws Exception {
		StringBuilder builder = new StringBuilder();
		char[] buffer = new char[4096];
		int readLength = reader.read(buffer);
		while (readLength >= 0) {
			builder.append(buffer, 0, readLength);
			readLength = reader.read(buffer);
		}
		return builder.toString();
	}
/**
 * 获取包名
 * @param context
 * @return
 */
	public static String getPackageName(Context context) {
		return context.getPackageName();
	}

	/**
	 *  获取应用程序名称
	 * @param context
	 * @return
	 */
	 
	public static String getAppName(Context context) {
		try {
			PackageManager packageManager = context.getPackageManager();
			PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
			int labelRes = packageInfo.applicationInfo.labelRes;
			return context.getResources().getString(labelRes);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

}
